package com.zcpt.controller;

import java.io.UnsupportedEncodingException;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.zcpt.pojo.Partner;
import com.zcpt.service.PartnerService;

@Controller
@RequestMapping("/partner")
public class PartnerController {

	@Resource
	private PartnerService partnerService;

	// 返回参与者id的信息
	@RequestMapping(value = "/{id}/part", method = RequestMethod.GET)
	public String getPartnerById(@PathVariable("id") Integer partnerId, Model model) {
		if (partnerId == null) {
			return "redirect:/partner/error";
		}
		Partner partner = partnerService.getPartnerById(partnerId);
		if (partner == null) {
			return "redirect:/partner/error";
		}
		model.addAttribute("part", partner);
		return "part";
	}

	// 返回所有参与者信息
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public String getAllPartner(Model model) {
		List<Partner> parList = partnerService.getAllPartner();
		model.addAttribute("list", parList);
		return "part_list";
	}

	// 返回参与者id和对应项目信息
	@RequestMapping(value = "/{id}/part_pro", method = RequestMethod.GET)
	public String getPartnerByIdWithProject(@PathVariable("id") Integer partnerId, Model model) {
		if (partnerId == null) {
			return "redirect:/partner/error";
		}
		Partner partner = partnerService.getPartnerByIdWithProject(partnerId);
		if (partner == null) {
			return "redirect:/partner/error";
		}
		model.addAttribute("part_pro", partner);
		return "part_pro";
	}

	// 添加参与者信息
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public String addPartner(HttpServletRequest req) {
		try {
			req.setCharacterEncoding("utf-8");
		} catch (UnsupportedEncodingException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
		String partnerName = req.getParameter("partName");
		String partnerGender = req.getParameter("partGender");
		String partnerPhone = req.getParameter("partPhone");
		Partner part = new Partner();
		part.setPartnerName(partnerName);
		part.setPartnerGender(partnerGender);
		part.setPartnerPhone(partnerPhone);
		if (partnerService.addPartner(part)) {
			return "add_success";
		} else {
			return "add_failure";
		}
	}

	// 参与者参与项目
	@RequestMapping(value = "/{partnerId}/join/{projectId}", method = RequestMethod.GET)
	public String partnerProject(@PathVariable("partnerId") Integer partnerId,
			@PathVariable("projectId") Integer projectId) {
		if (partnerId == null || projectId == null) {
			return "redirect:/partner/error";
		}
		if (partnerService.partnerProject(partnerId, projectId)) {
			return "rele_success";
		} else {
			return "rele_failure";
		}
	}

	// 修改参与者信息
	@RequestMapping(value = "/alter", method = RequestMethod.POST)
	public String alterPartner(HttpServletRequest req) {
		try {
			req.setCharacterEncoding("utf-8");
		} catch (UnsupportedEncodingException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
		int partnerId = Integer.parseInt(req.getParameter("partId"));
		Partner partner = partnerService.getPartnerById(partnerId);
		if (partner == null) {
			return "redirect:/partner/error";
		}
		String partnerName = req.getParameter("partName");
		String partnerGender = req.getParameter("partGender");
		String partnerPhone = req.getParameter("partPhone");
		Partner par = new Partner();
		par.setPartnerId(partnerId);
		par.setPartnerName(partnerName);
		par.setPartnerGender(partnerGender);
		par.setPartnerPhone(partnerPhone);
		if (partnerService.alterPartner(par)) {
			return "alter_success";
		} else {
			return "alter_failure";
		}
	}

	@RequestMapping(value = "/error")
	public String error() {
		return "error";
	}
}
